package cn.chinaiptv.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.chinaiptv.bean.RecordBean;

import com.google.gson.Gson;

/**
 * 解析平台参数信息的工具类
 * 
 * @author Admin
 *
 */
public class RecordUtils {
	/**
	 * 记录日志
	 * 
	 * @param bean
	 * @param filename
	 * @return
	 */
	public static boolean writeRecord(RecordBean bean) {
		FileInputStream fileInputStream = null;
		try {
			ArrayList<RecordBean> records = getRecords();
			records.add(0, bean);
			Gson gson = new Gson();
			String json = gson.toJson(records);
			writeData(ParamsFromConfFile.getRecordPath(), json);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;

	}

	/**
	 * 获取所有的日志信息
	 * 
	 * @param filename
	 * @return
	 */
	public static ArrayList<RecordBean> getRecords() {
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(
					ParamsFromConfFile.getRecordPath());
			String str = readToBuffer(fileInputStream);
			return parseRes(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 获取下一页数据
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public static ArrayList<RecordBean> getNextPage(int pageNum, int pageSize) {
		ArrayList<RecordBean> allBeans = getRecords();
		ArrayList<RecordBean> tempBeans = new ArrayList<RecordBean>();
		int pageCount;
		pageCount = (allBeans.size() + pageSize - 1) / pageSize;
		System.out.println(pageCount + "===");
		if (pageNum > pageCount) {
			return null;
		} else {
			if (pageCount == 1) {
				return allBeans;
			} else {
				int currentNum;
				for (int i = 0; i < pageSize; i++) {
					currentNum = (pageNum - 1) * pageSize + i;
					if (currentNum <= allBeans.size() - 1) {
						tempBeans.add(allBeans.get(currentNum));
					} else {
						break;
					}
				}
				return tempBeans;
			}
		}
	}

	/**
	 * 解析文件
	 * 
	 * @param str
	 * @return
	 */
	private static ArrayList<RecordBean> parseRes(String str) {
		ArrayList<RecordBean> beans = new ArrayList<RecordBean>();
		JSONArray array;
		try {
			array = new JSONArray(str);

			for (int i = 0; i < array.length(); i++) {
				try {
					JSONObject obj = array.getJSONObject(i);
					String time = obj.getString("time");
					String platform = obj.getString("platform");
					String group = obj.getString("group");
					String operation = obj.getString("operation");
					String fileName = obj.getString("fileName");
					String worksheedURl = obj.getString("worksheedURl");
					String status = obj.getString("status");
					RecordBean bean = new RecordBean(time, platform, group,
							operation, fileName, worksheedURl, status);
					beans.add(bean);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			return beans;
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return null;
	}

	/**
	 * 读取配置文件中的日志信息
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static String readToBuffer(FileInputStream is) throws IOException {
		String line; // 用来保存每行读取的内容
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		line = reader.readLine(); // 读取第一行
		StringBuffer buffer = new StringBuffer();
		while (line != null) { // 如果 line 为空说明读完了
			buffer.append(line); // 将读到的内容添加到 buffer 中
			buffer.append("\n"); // 添加换行符
			line = reader.readLine(); // 读取下一行
		}
		reader.close();
		is.close();
		return buffer.toString();
	}

	/**
	 * 写入文件
	 * 
	 * @param filePathString
	 * @param jsonData
	 */
	public static void writeData(String filePathString, String jsonData) {

		try {
			BufferedReader reader = new BufferedReader(new StringReader(
					jsonData));
			BufferedWriter writer = new BufferedWriter(new FileWriter(
					filePathString));
			int len = 0;
			char[] buffer = new char[1024];
			while ((len = reader.read(buffer)) != -1) {
				writer.write(buffer, 0, len);
			}
			writer.flush();
			writer.close();
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
